using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin;
using MicroMsg.Resource.@string;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class FindFriendView : UserControl
	{
		public const string bNotFirstInDriftBottleKey = "bNotFirstInDriftBottle";
		public const string bNotFirstInScanQRCodeKey = "bNotFirstInScanQRCode";
		private bool mBIsSetPlugins;
		private List<PluginBase> mListPlugins = new List<PluginBase>();
		private List<PluginBase> listHardCodePlugins = new List<PluginBase>();
		private PluginBaseEx pluginAddFreind;
		private PluginBase pluginQRCode;
		private static ApplicationBar mBar;
		private static ApplicationBarMenuItem settingButton;
		private static ApplicationBarMenuItem updateButton;
		internal Grid LayoutRoot;
		internal Grid ffMainGrid;
		internal Paragraph paraPluginHolder;
		internal StackPanel panelPluginHolder;
		private bool _contentLoaded;
		public FindFriendView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.FindFriendView_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.FindFriendView_Unloaded));
		}
		private void FindFriendView_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void FindFriendView_Loaded(object sender, RoutedEventArgs e)
		{
			this.refreshTabs();
			this.refreshPlugins();
		}
		private HubPluginCtrlBase addPlugToPara(int nIndex, PluginBase plugin)
		{
			InlineUIContainer inlineUIContainer = new InlineUIContainer();
			HubPluginCtrlBase hubPluginCtrlBase;
			if (plugin.mName == "Circle")
			{
				hubPluginCtrlBase = new HubPlugWideCtrl(true);
			}
			else
			{
				if (plugin.mName == "LBS")
				{
					hubPluginCtrlBase = new HubPlugWideCtrl(false);
				}
				else
				{
					hubPluginCtrlBase = new HubPluginCtrl();
				}
			}
			hubPluginCtrlBase.ItemObject = plugin;
			inlineUIContainer.set_Child(hubPluginCtrlBase);
			this.paraPluginHolder.get_Inlines().Insert(nIndex, inlineUIContainer);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				plugin.OnShowInFFEntry();
			});
			return hubPluginCtrlBase;
		}
		private void removePlugFromPara(int nIndex)
		{
			this.paraPluginHolder.get_Inlines().RemoveAt(nIndex);
		}
		private HubPluginCtrlBase addOnePlugin(PluginBase plugin)
		{
			bool flag = false;
			int indexOfPlugin = this.getIndexOfPlugin(plugin, ref flag);
			if (!flag)
			{
				if (-1 != indexOfPlugin)
				{
					this.mListPlugins.Insert(indexOfPlugin, plugin);
					return this.addPlugToPara(indexOfPlugin, plugin);
				}
			}
			return null;
		}
		private int getIndexOfPlugin(PluginBase plugin, ref bool bIsFoundSame)
		{
			bIsFoundSame = false;
			if (plugin == null)
			{
				return -1;
			}
			int num = plugin.mIndexInGroup * 10 + plugin.mIndexInPosition;
			for (int i = 0; i < this.mListPlugins.get_Count(); i++)
			{
				PluginBase pluginBase = this.mListPlugins.get_Item(i);
				if (pluginBase != null)
				{
					int num2 = pluginBase.mIndexInGroup * 10 + pluginBase.mIndexInPosition;
					if (num2 == num)
					{
						bIsFoundSame = true;
						return i;
					}
					if (num2 > num)
					{
						return i;
					}
				}
			}
			return 0;
		}
		private void removePlugin(PluginBase plugin)
		{
			bool flag = false;
			int indexOfPlugin = this.getIndexOfPlugin(plugin, ref flag);
			if (flag)
			{
				if (indexOfPlugin != -1)
				{
					this.mListPlugins.RemoveAt(indexOfPlugin);
					this.removePlugFromPara(indexOfPlugin);
					return;
				}
			}
		}
		private void matchPlugins(List<PluginBase> listPlugins)
		{
			if (listPlugins == null)
			{
				listPlugins = new List<PluginBase>();
			}
			using (List<PluginBase>.Enumerator enumerator = listPlugins.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					PluginBase current = enumerator.get_Current();
					if (current != null && !this.mListPlugins.Contains(current))
					{
						this.addOnePlugin(current);
					}
				}
			}
			for (int i = 0; i < this.mListPlugins.get_Count(); i++)
			{
				PluginBase pluginBase = this.mListPlugins.get_Item(i);
				if (pluginBase != null && !listPlugins.Contains(pluginBase) && !this.listHardCodePlugins.Contains(pluginBase))
				{
					this.removePlugin(pluginBase);
					i--;
				}
			}
		}
		private void getPlugins()
		{
			if (this.mBIsSetPlugins)
			{
				return;
			}
			this.mBIsSetPlugins = true;
			this.hardCodePlugins();
			List<PluginBase> list = ExtentCenter.getListInTab(MainTabIndex.FindFriendTab);
			if (list == null)
			{
				list = new List<PluginBase>();
			}
			using (List<PluginBase>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					PluginBase current = enumerator.get_Current();
					this.addOnePlugin(current);
				}
			}
		}
		private void refreshPlugins()
		{
			if (!this.mBIsSetPlugins)
			{
				this.getPlugins();
				return;
			}
			List<PluginBase> listInTab = ExtentCenter.getListInTab(MainTabIndex.FindFriendTab);
			this.matchPlugins(listInTab);
		}
		private void hardCodePlugins()
		{
			if (this.listHardCodePlugins.get_Count() != 0)
			{
				return;
			}
			this.pluginAddFreind = new PluginBaseEx
			{
				mIndexInGroup = 0,
				mIndexInPosition = 5,
				mFFEntryTitle = strings.FF_AddFriend_Title,
				mIconInFFEntry = ImageCacheMgr.loadFromRes(MMTheme.GetInstance().searchFriend_main)
			};
			this.listHardCodePlugins.Add(this.pluginAddFreind);
			HubPluginCtrlBase hubPluginCtrlBase = this.addOnePlugin(this.pluginAddFreind);
			if (hubPluginCtrlBase != null)
			{
				hubPluginCtrlBase.ItemClick += new HubPluginCtrlBase.ListItemClick(this.ctrlAddFreind_ItemClick);
			}
			PluginBase pluginBase = ExtentCenter.findPluginByID(32769L);
			hubPluginCtrlBase = this.addOnePlugin(pluginBase);
			this.listHardCodePlugins.Add(pluginBase);
			PluginBase pluginBase2 = ExtentCenter.findPluginByID(32771L);
			hubPluginCtrlBase = this.addOnePlugin(pluginBase2);
			this.listHardCodePlugins.Add(pluginBase2);
			BitmapImage mIconInFFEntry;
			if (MMTheme.IsCoolBlackTheme())
			{
				mIconInFFEntry = new BitmapImage(new Uri("/Source/Plugin/QRCode/Resource/scanQRCode.png", 2));
			}
			else
			{
				mIconInFFEntry = new BitmapImage(new Uri("/Source/Plugin/QRCode/Resource/scanQRCode_light.png", 2));
			}
			this.pluginQRCode = new PluginBaseEx
			{
				mIndexInGroup = 0,
				mIndexInPosition = 4,
				mFFEntryTitle = strings.FF_Entry_QRCode,
				mIconInFFEntry = mIconInFFEntry
			};
			hubPluginCtrlBase = this.addOnePlugin(this.pluginQRCode);
			hubPluginCtrlBase.ItemClick += new HubPluginCtrlBase.ListItemClick(this.btnScanQRCode_Click);
			this.listHardCodePlugins.Add(this.pluginQRCode);
		}
		private void ctrlAddFreind_ItemClick(object sender, object object_0)
		{
			RegionInfo arg_05_0 = RegionInfo.get_CurrentRegion();
			ToolFunction.GotoPageForUserControl("/Source/UI/AddFreindPage.xaml", null, false);
		}
		public static ApplicationBar GetApplicationBar(bool useCache = true)
		{
			if (FindFriendView.mBar != null && useCache)
			{
				if (!FindFriendView.mBar.get_IsVisible())
				{
					FindFriendView.mBar.set_IsVisible(true);
				}
				return FindFriendView.mBar;
			}
			FindFriendView.mBar = new ApplicationBar();
			FindFriendView.mBar.set_IsMenuEnabled(true);
			FindFriendView.mBar.set_IsVisible(true);
			FindFriendView.mBar.set_Mode(1);
			FindFriendView.settingButton = new ApplicationBarMenuItem(strings.COM_Setting);
			FindFriendView.settingButton.add_Click(new EventHandler(FindFriendView.settingButton_Click));
			FindFriendView.mBar.get_MenuItems().Add(FindFriendView.settingButton);
			if (MainPage.mainPageInstance != null && MainPage.mainPageInstance.m_UpdateInfo != null)
			{
				FindFriendView.updateButton = new ApplicationBarMenuItem(strings.Set_AboutMM_Update);
				FindFriendView.updateButton.add_Click(new EventHandler(FindFriendView.btnUpdateVersion_Click));
				FindFriendView.mBar.get_MenuItems().Add(FindFriendView.updateButton);
			}
			return FindFriendView.mBar;
		}
		public void OnLanguageChange()
		{
			if (FindFriendView.settingButton != null)
			{
				FindFriendView.settingButton.set_Text(strings.COM_Setting);
			}
			if (FindFriendView.updateButton != null)
			{
				FindFriendView.updateButton.set_Text(strings.Set_AboutMM_Update);
			}
			if (this.pluginAddFreind != null)
			{
				this.pluginAddFreind.mFFEntryTitle = strings.FF_AddFriend_Title;
			}
			if (this.pluginQRCode != null)
			{
				this.pluginQRCode.mFFEntryTitle = strings.FF_Entry_QRCode;
			}
			using (IEnumerator<Inline> enumerator = this.paraPluginHolder.get_Inlines().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Inline current = enumerator.get_Current();
					InlineUIContainer inlineUIContainer = current as InlineUIContainer;
					if (inlineUIContainer != null && inlineUIContainer.get_Child() != null)
					{
						HubPluginCtrlBase hubPluginCtrlBase = inlineUIContainer.get_Child() as HubPluginCtrlBase;
						if (hubPluginCtrlBase == null)
						{
							DebugEx.debugBreak();
						}
						else
						{
							hubPluginCtrlBase.OnLanguageChange();
						}
					}
					else
					{
						DebugEx.debugBreak();
					}
				}
			}
		}
		private static void btnUpdateVersion_Click(object sender, EventArgs e)
		{
			if (MainPage.mainPageInstance != null && MainPage.mainPageInstance.m_UpdateInfo != null)
			{
				string text = (MainPage.mainPageInstance.m_UpdateInfo.PackSize * 1.0 / 1048576.0).ToString();
				int num = (text.get_Length() > 3) ? 3 : text.get_Length();
				text = text.Substring(0, num);
				MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Set_AboutMM_HasNewVersion, string.Concat(new string[]
				{
					MainPage.mainPageInstance.m_UpdateInfo.PackDescription,
					"\r\n",
					strings.Set_AboutMM_PackageSize,
					text,
					"MB"
				}), true, strings.Set_AboutMM_Update, strings.COM_Cancel);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(MainPage.mainPageInstance.updateInfoDlgReturn);
			}
		}
		private static void settingButton_Click(object sender, EventArgs e)
		{
			MainPage.goToSetting();
		}
		private void LogoutButton_Click(object sender, EventArgs e)
		{
			MainPage.initLogOut();
		}
		private void btn_KeyDown(object sender, MouseButtonEventArgs e)
		{
		}
		private void btn_KeyUp(object sender, MouseButtonEventArgs e)
		{
		}
		private void btnSearchFriend_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
		}
		private void btnSearchFriend_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
		}
		private void btnSearchFriend_MouseLeave(object sender, MouseEventArgs e)
		{
		}
		private void btnSearchFriend_MouseEnter(object sender, MouseEventArgs e)
		{
		}
		private void refreshTabs()
		{
		}
		private void btnScanQRCode_Click(object sender, object object_0)
		{
			if (!ConfigMgr.getBool("bNotFirstInScanQRCode", false))
			{
				ToolFunction.GotoPageForUserControl("/Source/Plugin/QRCode/UI/QRCodeGuidePage.xaml", null, false);
				return;
			}
			ToolFunction.GotoPageForUserControl("/Source/Plugin/QRCode/UI/ScanQRCodePage.xaml", null, false);
		}
		private void btnShowQRCode_Click(object sender, RoutedEventArgs e)
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/QRCode/UI/ShowQRCodeListPage.xaml", null, false);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/FindFriendView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ffMainGrid = (Grid)base.FindName("ffMainGrid");
			this.paraPluginHolder = (Paragraph)base.FindName("paraPluginHolder");
			this.panelPluginHolder = (StackPanel)base.FindName("panelPluginHolder");
		}
	}
}
